package com.ruoyi.utils;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.traplight.domain.BufferedImageMultipartFile;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类作用：根据坐标点画检测框
 *
 * @author Mr-Le
 */
@Slf4j
public class ImgDrawCircleTool {

    /**
     * 褐飞虱
     */
    public static final String HFS = "hfs";

    /**
     * 白背飞虱
     */
    public static final String BBFS = "bbfs";

    public static final HashMap<String , Color> TagColorHashMap = new HashMap<>();
    static {
        // 初始化映射关系
        TagColorHashMap.put("y1", new Color(255, 0, 0) ); //红色 白背飞虱
        TagColorHashMap.put("y2", new Color(0, 255, 0) );//绿色 褐飞虱
        TagColorHashMap.put("y3", new Color(0, 0, 255));//蓝色 二化螟
        TagColorHashMap.put("y4", new Color(255, 255, 0) );//黄色 稻纵卷叶螟
        TagColorHashMap.put("y5", new Color(0, 255, 255) );//青色 玉米螟
        TagColorHashMap.put("y6", new Color(255, 0, 255) );//品红 草地螟
        TagColorHashMap.put("y7", new Color(128, 128, 128) );//灰色 东方粘虫
        TagColorHashMap.put("y8", new Color(255, 128, 0) );//橙色 劳氏粘虫
        TagColorHashMap.put("y9", new Color(40, 255, 128) );//亮绿色 草地贪夜蛾
        TagColorHashMap.put("e1", new Color(128, 0, 255) );//紫色 大螟
        TagColorHashMap.put("e2", new Color(0, 128, 255) );//天蓝色 斜纹夜蛾
        TagColorHashMap.put("e3", new Color(255, 255, 255));//白色 棉铃虫
        TagColorHashMap.put("e4", new Color(0, 0, 0));//黑色 小菜蛾
        TagColorHashMap.put("e5", new Color(255, 192, 203));//粉色 桃蛀螟
        TagColorHashMap.put("e6", new Color(0, 128, 0));//深绿色 梨小食心虫
        TagColorHashMap.put("e7", new Color(255, 165, 0));//橙黄色 小地老虎
        TagColorHashMap.put("e8", new Color(255, 255, 224));//浅黄色 八字地老虎
        TagColorHashMap.put("e9", new Color(139, 69, 19));//巧克力色 铜绿丽金龟
        TagColorHashMap.put("e10", new Color(218, 165, 32));//金色 大黑鳃金龟
        TagColorHashMap.put("e11", new Color(0, 139, 139));//深青色 甜菜夜蛾
        TagColorHashMap.put("e12", new Color(139, 0, 139));//深紫色 二点委夜蛾
        TagColorHashMap.put("q1", new Color(70, 130, 180));//钢蓝色 绿盲蝽
        TagColorHashMap.put("q2", new Color(240, 128, 128));//淡红色 三化螟
        TagColorHashMap.put("q3", new Color(32, 178, 170));//绿宝石绿 小绿叶蝉
        TagColorHashMap.put("q4", new Color(255, 69, 0));//红橙色 叩甲
        TagColorHashMap.put("q5", new Color(255, 218, 185));//粉晶色 暗黑鳃金龟
        TagColorHashMap.put("q6", new Color(154, 205, 50));//黄绿色 桑绢野螟
        TagColorHashMap.put("q7", new Color(0, 100, 0));//深绿色 警纹地老虎
        TagColorHashMap.put("q8", new Color(205, 92, 92));//印度红 黄字地老虎
        TagColorHashMap.put("q9", new Color(255, 20, 147));//深粉色 电光叶蝉
        TagColorHashMap.put("q10", new Color(160, 32, 240));//紫罗兰色 稻黑蝽
        TagColorHashMap.put("q11", new Color(128, 128, 0));//橄榄色 黄足猎蝽
        TagColorHashMap.put("q12", new Color(255, 228, 196));//浅肉色 草蛉类
        TagColorHashMap.put("q13", new Color(255, 182, 193));//浅粉色 瓜绢螟
        TagColorHashMap.put("q14", new Color(173, 216, 230));//淡蓝色 甜菜白带野螟
        TagColorHashMap.put("q15", new Color(0, 0, 128));//海军蓝色 青翅蚁形隐翅甲
        TagColorHashMap.put("q16", new Color(255, 99, 71));//番茄红 蟋蟀类
        TagColorHashMap.put("q17", new Color(128, 0, 0));//红褐色 瓢甲类
        TagColorHashMap.put("q18", new Color(0, 128, 128));//绿松石绿 蝼蛄类
        TagColorHashMap.put("x1", new Color(218, 112, 214));//蓝紫色 线委夜蛾
        TagColorHashMap.put("x2", new Color(255, 255, 0));//鲜黄色 苹果蠹蛾
        TagColorHashMap.put("x3", new Color(100, 149, 237));//石板蓝色 松毛虫
        TagColorHashMap.put("a10",new Color(100, 250, 40));//金黄镰翅野螟
        TagColorHashMap.put("e120", new Color(0, 255, 127));//春绿色 橄榄绿尾尺蛾
        TagColorHashMap.put("e90", new Color(255, 140, 0));//橙红色 榆黄足毒蛾
        TagColorHashMap.put("e40", new Color(255, 105, 180));//热情粉红 杨雪毒蛾
        TagColorHashMap.put("a41", new Color(255, 250, 205));//杏色 格蔗尺蛾

    }

    /**
     * 大螟
     */
//    public static final String A21 = "a21";

    /**
     * 稻纵卷叶螟
     */
//    public static final String A12 = "a12";

    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath, Integer num1, Integer num2,Integer num3, Integer num4,Integer num5,String imgPest) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

//        if (list.isEmpty()) {
//            return null;
//        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = Color.RED;
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    // 增加图例，每种虫有多少头显示 替换
                    int num=num1+num2+num3+num4+num5;
                    String numData1 = imgPest+":" + num;

                    int font_size = image.getWidth()/300*6;
                    g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    // 增加图例，每种虫有多少头显示
                    //g.setFont(new Font("宋体", Font.BOLD, 64));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    //int letfX = font_size*13;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);

                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() -   font_size*numData1.length(), baseY);


                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }



    /**
     * 根据坐标点、标签集合画 检测框
     * 这是所有识别目标当做imgPest目标的绘图
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath, Integer num,String imgPest) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

//        if (list.isEmpty()) {
//            return null;
//        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                if ((list.size()==0 && tag==null) || list.size() == tag.size() ) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = Color.RED;
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    // 增加图例，每种虫有多少头显示 替换

                    String numData1 = imgPest+":" + num;

                    int font_size = image.getWidth()/300*6;
                    g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    // 增加图例，每种虫有多少头显示
                    //g.setFont(new Font("宋体", Font.BOLD, 64));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    //int letfX = font_size*13;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);

                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() - font_size*numData1.length(), baseY);


                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据坐标点、标签集合画 检测框
     * 这是性诱分成2种类别的情况，目标类别和非目标类别
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath, Integer tagCount,Integer tagOtherCount,String imgPest,boolean flagForPestIsModel) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        String pinYinImgPest = getPinYinHeadChar(imgPest);

//        if (list.isEmpty()) {
//            return null;
//        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                if ((list.size()==0 && tag==null) || list.size() == tag.size() ) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = Color.RED;
                        if(flagForPestIsModel){
                            if(tag.get(i).equals(pinYinImgPest)){
                                newColor = Color.RED;
                            }else {
                                newColor = Color.BLUE;
                            }
                        }else {
                            newColor = Color.RED;
                        }
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    // 增加图例，每种虫有多少头显示 替换

                    String numData1 = imgPest+":" + tagCount;
                    String numData2 = "非目标害虫:" + tagOtherCount;

                    int font_size = image.getWidth()/300*6;
                    g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    // 增加图例，每种虫有多少头显示
                    //g.setFont(new Font("宋体", Font.BOLD, 64));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    //int letfX = font_size*13;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);

                    int maxLength=numData1.length()>numData2.length()?numData1.length():numData2.length();

                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() - font_size*maxLength, baseY);

                    g.setColor(Color.BLUE);
                    g.drawString(numData2, image.getWidth() - font_size*maxLength, baseY*2);


                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
    // 球机图片绘制结果图和绘制框
    public String doDrawRectangle(List<int[]> list, List<String> tag,Map<String, Integer> resMap,String inImgPath, String manual_id) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

//        if (list.isEmpty()) {
//            return null;
//        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult2(inImgPath, manual_id);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(7.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = judgeColorByTag(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }

                    String Tag1=  "二化螟:";
                    String num1 = resMap.containsKey("ehm")?resMap.get("ehm").toString():"0";
                    String Tag2=  "稻纵卷叶螟:";
                    String num2 = resMap.containsKey("dzjym")?resMap.get("dzjym").toString():"0";
                    String Tag3=  "稻曲病:";
                    String num3 = resMap.containsKey("dqb")?resMap.get("dqb").toString():"0";
                    int font_size = image.getWidth()/300*6;
                    g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    int letfX = font_size*11;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);
                    int deta = font_size*9+5;
                    g.setColor(judgeColorByTag("ehm"));
                    g.drawString(Tag1, image.getWidth() - letfX, baseY);
                    g.drawString(num1, image.getWidth() - letfX+deta, baseY);
                    g.setColor(judgeColorByTag("dzjym"));
                    g.drawString(Tag2, image.getWidth() - letfX, baseY*2);
                    g.drawString(num2, image.getWidth() - letfX+deta, baseY*2);
                    g.setColor(judgeColorByTag("dqb"));
                    g.drawString(Tag3, image.getWidth() - letfX, baseY*3);
                    g.drawString(num3, image.getWidth() - letfX+deta, baseY*3);

                    // 先释放资源
                    g.dispose();
                    // 然后在图片上绘制人工id
                    image = ImageUtil.addDeviceId(manual_id, image);

                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**


     /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath,String imgPest) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(7.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = Color.RED;
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    String numData1 = imgPest+":" + tag.size();

                    int font_size = image.getWidth()/300*6;
                    //g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    // 增加图例，每种虫有多少头显示
                    g.setFont(new Font("宋体", Font.BOLD, 64));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    //int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    //int letfX = font_size*13;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);

                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() - 500, 80);
                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }
    /**
     * 根据坐标点、标签集合画 检测框 用于测报灯画框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
    public String doDrawRectangleT(List<int[]> list, List<String> tag, String inImgPath) {
        String resPath = null;
//        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
//                int fontSize = image.getWidth() / 40; // 根据图片宽度计算字体大小，这里使用图片宽度的 1/10
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
//                g.setColor(Color.white);
                g.setStroke(new BasicStroke(8.0f));
//                g.setFont(new Font("Arial", Font.PLAIN, fontSize)); // 设置字体样式和大小
//                int x = 10;
//                int y = fontSize;
//                g.drawString(timestamp, x, y); // 在指定位置绘制时间信息
//                log.info("11132412521");
                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
//                        Color newColor = judgeColorByTagT(tag.get(i));
                        Color newColor = TagColorHashMap.get(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
//                        new model
//                        g.drawRect(list.get(i)[1], list.get(i)[0], list.get(i)[3] - list.get(i)[1], list.get(i)[2] - list.get(i)[0]);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                log.info("errroooor" + e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据用户id绘制 该用户定制的虫情的结果图
     * @param list
     * @param tag
     * @param inImgPath
     * @return
     */
    public String doDrawRectangleForUnit(Integer unitId,List<int[]> list, List<String> tag, String inImgPath) {

        String resPath = null;
        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            resPath = addUnitId(resPath,unitId);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));

                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(14.0f));
                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = TagColorHashMap.get(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                log.info("errroooor" + e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }
    /**

     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangleAll0(String inImgPath,String imgPest) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));


                // 增加图例，每种虫有多少头显示 替换
                String Data1 = imgPest;


                String numData1=Data1+":0";


                int font_size = image.getWidth()/300*6;
                g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                // 增加图例，每种虫有多少头显示
                //g.setFont(new Font("宋体", Font.BOLD, 64));
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

                int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                int letfX = font_size*13;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);

                g.setColor(Color.RED);
                g.drawString(numData1, image.getWidth() - font_size*numData1.length(), baseY);

                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                ImageIO.write(image, "jpeg", fileOutputStream);
                fileOutputStream.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTagT(String tag) {

        switch (tag) {
//            case "z0"://白背飞虱
//                return new Color(247,80,0);
//            case "z1"://褐飞虱
//                return new Color(131,53,0);
//            case "z2"://RLF稻纵卷叶螟
//                return new Color(40,255,40);
//            case "z3"://Rove beetle青翅蚁形隐翅甲
//                return new Color(217,135,25);
//            case "z4"://Cricket蟋蟀类
//                return new Color(147,112,219);
//            case "z5"://Beet webworm甜菜白带野螟
//                return new Color(127,0,255);
//            case "z6"://Lady beetles瓢甲类
//                return new Color(40,148,255);
//            case "z7"://RSB二化螟
//                return new Color(0, 0, 64);
//            case "z8"://Melon silk moth瓜绢螟
//                return new Color(64, 00, 0);
//            case "z9"://Cotton leaf worm斜纹夜蛾类
//                return new Color(188,23,23);
//            case "z10"://Boll worm棉铃虫
//                return new Color(0, 128, 64);
//            case "z11"://YFB黄足猎蝽
//                return new Color(0, 86, 128);
//            case "z12"://Lacewing草蛉类
//                return new Color(140,120,83);
//            case "z13"://Rice bug稻黑蝽
//                return new Color(225, 225, 128);
//            case "z14"://粘虫
//                return new Color(255, 135, 255);

            case "hfs"://褐飞虱属
                return Color.RED;
            case "bbfs"://白背飞虱
                return Color.GREEN;
            case "a1"://蝼蛄
                return Color.PINK;
            case "a2"://二化螟
                return Color.CYAN;
            case "a3"://铜绿丽金龟
                return Color.GRAY;
            case "a4"://草地螟
                return Color.ORANGE;
            case "a10"://金黄镰翅野螟
                return Color.MAGENTA;
            case "a12"://稻纵卷叶螟
                return Color.YELLOW;
            case "a21"://大螟
                return Color.BLUE;
            case "a102"://棉铃虫
                return Color.black;
            case "a122"://粘虫
                return new Color(112, 128, 105);
            case "a22"://玉米螟
                return new Color(128, 128, 105);
            case "a41"://格蔗尺蛾
                return new Color(255, 153, 18);
            case "b13"://灰胸突鳃金龟
                return new Color(255, 227, 132);
            case "b28"://大黑鳃金龟
                return new Color(176, 224, 230);
            case "c21"://黄足猎蝽
                return new Color(65, 105, 225);
            case "e12"://烟青虫
                return new Color(106, 90, 205);
            case "e40"://杨雪毒蛾
                return new Color(135, 206, 235);
            case "e90"://榆黄足毒蛾
                return new Color(250, 235, 215);
            case "e120"://橄榄绿尾尺蛾
                return new Color(255, 235, 205);
            case "g21"://蟋蟀
                return new Color(227, 168, 105);
            default:
                return Color.WHITE;
        }
        //new model
//        switch (tag) {
//            case "0"://dzj
//                return Color.red;
//            case "1"://bbfs
//                return Color.GREEN;
//            case "2"://hfs
//                return Color.YELLOW;
//            case "3"://ehm
//                return Color.CYAN;
//            default:
//                return Color.GRAY;
//        }
    }

    /**
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTag(String tag) {
        switch (tag) {
            case "ehm":
                return Color.BLUE;
            case "dzjym":
                return new Color(222,118,131);
            case "dqb":
                return Color.RED;
            case "cdtye":
                return Color.MAGENTA;
            case "dm":
                return Color.RED;

            case HFS:
                return Color.red;
            case BBFS:
                return Color.GREEN;
            case "a1":
                return Color.YELLOW;
            case "a2":
                return Color.LIGHT_GRAY;
            case "a3":
                return Color.GRAY;
            case "a4":
                return Color.ORANGE;
            case "a10":
                return Color.MAGENTA;
            case "a12":
                return Color.PINK;
            case "a21":
                return Color.BLUE;
            case "a102":
                return Color.CYAN;
            case "a122":
                return new Color(112, 128, 105);
            case "a22":
                return new Color(128, 128, 105);
            case "a41":
                return new Color(255, 153, 18);
            case "b13":
                return new Color(255, 227, 132);
            case "b28":
                return new Color(176, 224, 230);
            case "c21":
                return new Color(65, 105, 225);
            case "e12":
                return new Color(106, 90, 205);
            case "e40":
                return new Color(135, 206, 235);
            case "e90":
                return new Color(250, 235, 215);
            case "e120":
                return new Color(255, 235, 205);
            case "g21":
                return new Color(227, 168, 105);

            default:
                return new Color(255, 99, 71);
        }
    }

    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        convert = convert.replaceAll("[^A-Z^a-z^0-9]", "");
        return convert;
    }

    /**
     * 新测报灯
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTagNew(String tag) {
        switch (tag) {
            case "z0"://白背飞虱
                return new Color(247, 80, 0);
            case "z1"://褐飞虱
                return new Color(131, 53, 0);
            case "z2"://RLF稻纵卷叶螟
                return new Color(40, 255, 40);
            case "z3"://Rove beetle青翅蚁形隐翅甲
                return new Color(217, 135, 25);
            case "z4"://Cricket蟋蟀类
                return new Color(147, 112, 219);
            case "z5"://Beet webworm甜菜白带野螟
                return new Color(127, 0, 255);
            case "z6"://Lady beetles瓢甲类
                return new Color(40, 148, 255);
            case "z7"://RSB二化螟
                return new Color(0, 0, 64);
            case "z8"://Melon silk moth瓜绢螟
                return new Color(64, 00, 0);
            case "z9"://Cotton leaf worm斜纹夜蛾类
                return new Color(188, 23, 23);
            case "z10"://Boll worm棉铃虫
                return new Color(0, 128, 64);
            case "z11"://YFB黄足猎蝽
                return new Color(0, 86, 128);
            case "z12"://Lacewing草蛉类
                return new Color(140, 120, 83);
            case "z13"://Rice bug稻黑蝽
                return new Color(225, 225, 128);
            case "z14"://粘虫
                return new Color(255, 135, 255);
            default:
                return Color.WHITE;
        }
    }

    /**
     * 添加标签：_Result 标签
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addResult(String srcPath) {
        String res = "";
        String[] str = new String[2];
        /* \\. 转义字符，相当于 . ,分隔出.前后的内容 */
        str = srcPath.split("\\.");
        res = str[0] + "__Result." + str[1];
        return res;
    }
    /**
     * 添加标签：_Result 标签,并将图片重命名为人工ID+时间
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addResult2(String srcPath,String manual_id) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());
        String day_str = formatter.format(date);
        String img_name = manual_id+"_"+day_str;
        String [] src = srcPath.split("\\.");
        String str = srcPath.substring(0,srcPath.lastIndexOf("/")) + "/"+img_name+ "__Result." + src[1];
        return str;
    }
    /**
     * 添加标签：__UserId 标签
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addUnitId(String srcPath,Integer unitId) {
        String res = "";
        String[] str = new String[2];
        /* \\. 转义字符，相当于 . ,分隔出.前后的内容 */
        str = srcPath.split("\\.");
        res = str[0] + String.format("__%d.",unitId) + str[1];
        return res;
    }


//    /**
//     * 根据坐标点集合画 检测框
//     * @param list 坐标点集合
//     * @param inImgPath 输入图片路径
//     * @return 返回结果图路径
//     */
//    public String doDrawRectangle(List<int[]> list, String inImgPath) {
//        String resPath = null;
//        String s = StringUtils.substringAfter(inImgPath, "profile");
//
//        File file = new File(inImgPath);
//
//        if (list.isEmpty()) {
//            return null;
//        }
//        if (file.exists()) {
//            System.out.println("file exists!");
//            // 原图加上 result 标签
//            resPath = addResult(inImgPath);
//            try {
//                BufferedImage image = ImageIO.read(new File(inImgPath));
//                Graphics graphics = image.getGraphics();
//                Graphics2D g = (Graphics2D) graphics;
//                g.setColor(Color.red);
//                g.setStroke(new BasicStroke(7.0f));
//                for (int[] ints : list) {
//                    // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
//                    g.drawRect(ints[0], ints[1], ints[2] - ints[0], ints[3] - ints[1]);
//                }
//                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
//                ImageIO.write(image, "jpeg", fileOutputStream);
//            } catch (Exception e) {
//                System.out.println(e.getMessage());
//            }
//        } else {
//            System.out.println("文件不存在");
//            return null;
//        }
//        return resPath;
//    }


}
